package com.tan.androidtest.customview.customviewgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * 测试自定义ViewGroup
 */

public class DemoViewGroup extends ViewGroup {


    public DemoViewGroup(Context context) {
        super(context);
    }

    public DemoViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DemoViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int selfWidthMeasureMode = MeasureSpec.getMode(widthMeasureSpec);
        int selfWidthMeasureSize = MeasureSpec.getSize(widthMeasureSpec);

        if (selfWidthMeasureMode == MeasureSpec.AT_MOST) {
            Log.i("==--", "AT_MOST");
        } else if (selfWidthMeasureMode == MeasureSpec.UNSPECIFIED) {
            Log.i("==--", "UNSPECIFIED");
        } else if (selfWidthMeasureMode == MeasureSpec.EXACTLY) {
            Log.i("==--", "EXACTLY");
        }

        measureChildren(widthMeasureSpec, heightMeasureSpec);

        /*for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            int childMeasureWidth = childView.getMeasuredWidth();
            int childMeasureHeight = childView.getMeasuredHeight();
            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childMeasureWidth, MeasureSpec.EXACTLY);
            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childMeasureHeight, MeasureSpec.EXACTLY);
            childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);

        }*/

        setMeasuredDimension(selfWidthMeasureSize, selfWidthMeasureSize);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int fMeasureWidth = getMeasuredWidth();
        //int fMeasureHeight=getMeasuredHeight();
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            int cMeasureWidth = childView.getMeasuredWidth();
            int cMeasureHeight = childView.getMeasuredHeight();

            int cl = 0, ct = 0, cr = 0, cb = 0;
            if (i == 0) {
                cl = 0;
                ct = 0;
                cr = cMeasureWidth;
                cb = cMeasureHeight;
            } else if (i == 1) {
                cl = fMeasureWidth - cMeasureWidth;
                ct = 0;
                cr = fMeasureWidth;
                cb = cMeasureHeight;
            }

            childView.layout(cl, ct, cr, cb);
        }
    }


}
